﻿@{ Layout = "~/Views/Shared/_FormWhite.cshtml"; }

<div class="wrapper animated fadeInRight">
    <form id="form" class="form-horizontal m">
        <div class="tabs-container">
            <ul class="nav nav-tabs">
                <li class="active">
                    <a data-toggle="tab" href="#tab-1" aria-expanded="true"> 档案周转</a>
                </li>
                <li class="">
                    <a data-toggle="tab" href="#tab-2" aria-expanded="false">重打盒码</a>
                </li>
            </ul>
            <div class="tab-content">
                <div id="tab-1" class="tab-pane active">
                    <div class="panel-body">
                        <div class="form-group">
                            <label class="col-sm-3 control-label ">档案类别：</label>
                            <div class="col-sm-8 select2" id="archive-type" col="ArchiveType">
                            </div>
                        </div>
                        @*<div class="form-group">
                                <label class="col-sm-3 control-label ">档案子类：</label>
                                <div class="col-sm-8">
                                    <input id="archiveid" col="Archiveid" type="text" class="form-control" placeholder="输入档案的子类，例如：DK； 也可为空" />
                                </div>
                            </div>*@

                        <div class="form-group">
                            <label class="col-sm-3 control-label ">档案年份：</label>
                            <div class="col-sm-8 select2" id="archive-year" col="ArchiveYear">
                            </div>
                        </div>

                        @*revised by MYL 2021-7-7*@

                        <div class="form-group">
                            <label class="col-sm-3 control-label ">档案号码：</label>
                            <div style="display:flex">
                                <div class="col-sm-4">
                                    <input id="archive-id-start" col="ArchiveIdStart" onkeyup="checkContent(this)" type="text" class="form-control" onblur="" placeholder="起始档案号" />
                                </div>
                                <div>~</div>
                                <div class="col-sm-4">
                                    <input id="archive-id-end" col="ArchiveIdEnd" onkeyup="checkContent(this)" type="text" class="form-control" placeholder="截止档案号" />
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label ">档案子号：</label>
                            <div style="display:flex">
                                <div class="col-sm-4">
                                    <input id="archive-id-start-child" col="ArchiveIdStartChild" onkeyup="checkContent(this)" type="text" class="form-control" placeholder="起始档案子号" />
                                </div>
                                <div>~</div>
                                <div class="col-sm-4">
                                    <input id="archive-id-end-child" col="ArchiveIdEndChild" onkeyup="checkContent(this)" type="text" class="form-control" placeholder="截止档案子号" />
                                </div>
                            </div>
                        </div>

                        @*档案列表*@
                        <div class="row" style="text-align:center">
                            <div class="col-sm-5"></div>
                            <button id="focobtn" type="button" class="btn btn-outline btn-info visible col-sm-2" onclick="generateArcIds()">生成档案号列表</button>
                        </div>
                        <div id="focotable" class="row visible" align="center">
                            <div class="col-sm-auto" style="padding-top:10px;text-align:center">
                                <div class="panel panel-primary ">
                                    <div class="panel-heading">
                                        <big>档案号列表（您可以选择需要删除的档案）</big>
                                    </div>
                                    <div id="archiveids" class="panel-body" style="column-count:2">

                                    </div>
                                </div>
                            </div>
                        </div>

                        @*批量打印*@
                        <div class="form-group" style="margin-bottom:-15px">
                            <div class="col-sm-12">
                                <label class="checkAll"><input type="checkbox" id="all" value="1" name="executeRadio">批量打印</label>
                                <p>
                                    <button type="button" id="btnPrintBoxCode" class="btn btn-block btn-outline btn-primary" onclick="printBoxCode()">打印一个盒码</button>
                                </p>
                                <div class="panel panel-default">
                                    <div class="panel-heading">
                                        <h4 class="panel-title">
                                            <a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">
                                                未归属到档案箱的盒码列表
                                            </a>
                                        </h4>
                                    </div>
                                    <div id="collapseTwo" class="panel-collapse collapse">
                                        <div class="panel-body">
                                            <span id="description" col="Description"></span>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div id="divBatch">
                            <div class="form-group">
                                <div class="col-sm-12" id="divBatch">

                                    <h3 class="font-weight-bold">
                                        <span id="curbincode" col="CurBinCode"></span>
                                        <span class="hidden" id="entityid" col="EntityId"></span>

                                    </h3>
                                    <p>
                                        <button type="button" id="btnPrintBinCode" class="btn btn-block btn-outline btn-primary" onclick="BatchprintBoxCode()">批量打印</button>
                                    </p>
                                </div>
                            </div>
                        </div>

                        <div id="divBottom">
                            <div id="divSecure">
                                <div class="form-group">
                                    <h3 class="font-weight-bold">
                                        <span id="curbincode" col="CurBinCode"></span>
                                        <span class="hidden" id="entityid" col="EntityId"></span>
                                        <span class="hidden" id="arcIds" col="ArcIds"></span>
                                    </h3>
                                    <label class="col-sm-3 control-label ">输入周转箱号码：</label>
                                    <div class="col-sm-8">
                                        <input id="tempbincode" col="TempBinCode" type="text" class="form-control" />
                                    </div>
                                </div>
                                <p>
                                    <button type="button" class="btn btn-block btn-outline btn-primary" onclick="bindTempBinCode()">完成，继续</button>
                                </p>
                            </div>
                        </div>

                    </div>
                </div>
                <div id="tab-2" class="tab-pane">
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-sm-12" id="box-list-for-reprint">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
</div>



<script type="text/javascript">

    $('#collapseTwo').collapse('show');
    $('#focobtn').attr('disabled', true);
    $("#focotable").css("visibility", "hidden");


      function print(code) {
        var flag;
        if (code.indexOf("-H-") > 0) flag = "盒码";
        else   flag = "箱码";

        ys.confirm("确定要重打" + flag + ":" + code + "吗？", function () {
                ys.ajax({
                    url: '@Url.Content("~/StorageManage/BarLib/ReprintCode")' + '?code=' + code,
                    type: "post",
                    success: function (obj) {
                        if (obj.Tag == 1) {
                            ys.msgSuccess(obj.Message);
                        }
                        else {
                            ys.msgError(obj.Message);
                        }
                    }
                });
        });
    }

    $(function () {
        var data = [
            { 'Key': '2020', 'Value': '2020' },
            { 'Key': '2019', 'Value': '2019' },
            { 'Key': '2018', 'Value': '2018' },
            { 'Key': '2017', 'Value': '2017' },
            { 'Key': '2016', 'Value': '2016' },
            { 'Key': '2015', 'Value': '2015' },
            { 'Key': '2014', 'Value': '2014' },
            { 'Key': '2013', 'Value': '2013' },
            { 'Key': '2012', 'Value': '2012' },
            { 'Key': '2011', 'Value': '2011' },
            { 'Key': '2010', 'Value': '2010' },
            { 'Key': '2009', 'Value': '2009' },
            { 'Key': '2008', 'Value': '2008' },
            { 'Key': '2007', 'Value': '2007' },
            { 'Key': '2006', 'Value': '2006' },
            { 'Key': '2005', 'Value': '2005' },
            { 'Key': '2004', 'Value': '2004' },
            { 'Key': '2003', 'Value': '2003' },
            { 'Key': '2002', 'Value': '2002' },
            { 'Key': '2001', 'Value': '2001' },
            { 'Key': '2000', 'Value': '2000' }
        ];

        var groupData = [
            {
                'Key': '规划类',
                'Value': [
                    { 'Key': 'Z49', 'Value': 'Z49' },
                    { 'Key': 'Z21', 'Value': 'Z21' },
                    { 'Key': 'Z22', 'Value': 'Z22' },
                    { 'Key': 'Z41', 'Value': 'Z41' },
                    { 'Key': 'Z43', 'Value': 'Z43' },
                    { 'Key': 'Z44', 'Value': 'Z44' },
                    { 'Key': 'ZQ', 'Value': 'ZQ' }
                ]
            },
            {
                'Key': '国土类',
                'Value': [
                    { 'Key': 'Z31', 'Value': 'Z31' },
                    { 'Key': 'Z32', 'Value': 'Z32' },
                    { 'Key': 'Z33', 'Value': 'Z33' },
                    { 'Key': 'Z34', 'Value': 'Z34' },
                    { 'Key': 'Z35', 'Value': 'Z35' },
                    { 'Key': 'Z36', 'Value': 'Z36' },
                    { 'Key': 'Z37', 'Value': 'Z37' },
                    { 'Key': 'Z38', 'Value': 'Z38' },
                    { 'Key': 'ZG', 'Value': 'ZG' },
                    { 'Key': 'ZT', 'Value': 'ZT' },
                    { 'Key': 'ZC', 'Value': 'ZC' },
                    { 'Key': 'ZJ', 'Value': 'ZJ' },
                    { 'Key': 'ZB', 'Value': 'ZB' },
                    { 'Key': 'ZH', 'Value': 'ZH' },
                    { 'Key': 'ZZ1', 'Value': 'ZZ1' },
                    { 'Key': 'ZZ2', 'Value': 'ZZ2' }
                ]
            },
            {
                'Key': '文书类',
                'Value': [
                    { 'Key': 'ZW', 'Value': 'ZW' },
                    { 'Key': 'ZW1', 'Value': 'ZW1' },
                    { 'Key': 'ZW2', 'Value': 'ZW2' }
                ]
            },
            {
                'Key': '房产类',
                'Value': [
                    { 'Key': 'Z51', 'Value': 'Z51' },
                    { 'Key': 'Z52', 'Value': 'Z52' },
                    { 'Key': 'Z54', 'Value': 'Z54' },
                    { 'Key': 'Z55', 'Value': 'Z55' },
                    { 'Key': 'Z56', 'Value': 'Z56' }
                ]
            },
            {
                'Key': '物业类',
                'Value': [
                    { 'Key': 'ZY', 'Value': 'ZY' },
                    { 'Key': 'ZY1', 'Value': 'ZY1' },
                    { 'Key': 'ZY2', 'Value': 'ZY2' },
                    { 'Key': 'ZY3', 'Value': 'ZY3' },
                    { 'Key': 'ZY4', 'Value': 'ZY4' },
                    { 'Key': 'ZY9', 'Value': 'ZY9' }
                ]

            },
            {
                'Key': '其他',
                'Value': [
                    { 'Key': 'ZQ', 'Value': 'ZQ' },
                    { 'Key': 'Z35', 'Value': 'Z35' },
                    { 'Key': 'ZT', 'Value': 'ZT' },
                    { 'Key': 'ZF1', 'Value': 'ZF1' },
                    { 'Key': 'ZFA3', 'Value': 'ZFA3' },
                    { 'Key': 'Z9221', 'Value': 'Z9221' },
                    { 'Key': 'Z9225', 'Value': 'Z9225' },
                    { 'Key': 'Z9226', 'Value': 'Z9226' },
                    { 'Key': 'Z9227', 'Value': 'Z9227' },
                    { 'Key': 'Z9211', 'Value': 'Z9211' },
                    { 'Key': 'Z9212', 'Value': 'Z9212' },
                    { 'Key': 'Z9213', 'Value': 'Z9213' },
                    { 'Key': 'Z9214', 'Value': 'Z9214' },
                    { 'Key': 'Z9215', 'Value': 'Z9215' },
                    { 'Key': 'Z9221', 'Value': 'Z9221' },
                    { 'Key': 'Z9222', 'Value': 'Z9222' },
                    { 'Key': 'Z9223', 'Value': 'Z9223' },
                    { 'Key': 'Z9311', 'Value': 'Z9311' },
                    { 'Key': 'Z9312', 'Value': 'Z9312' },
                    { 'Key': 'Z9313', 'Value': 'Z9313' },
                    { 'Key': 'Z9321', 'Value': 'Z9321' },
                    { 'Key': 'Z9351', 'Value': 'Z9351' },
                    { 'Key': 'Z9361', 'Value': 'Z9361' },
                    { 'Key': 'ZFB1', 'Value': 'ZFB1' },]
            },

        ];

        var CountBox;

        $("#archive-year").ysComboBox({
            data: data,
            class: "form-control"
        });

        $("#archive-type").ysComboBox({
            data: groupData,
            class: "form-control"
        });

        $('#form').validate({
            rules: {
                "archive-type": { required: true },
                "archive-year": { required: true },
                "archive-id-start": { required: true, digits: true },
                "archive-id-end": { digits: true },
                "archive-id-start-child": { digits: true },
                "archive-id-end-child": { digits: true },
            }
        });

      //  $("#divBottom").css("visibility", "hidden");

        init();
    });

     // 批量打印
    var radioCount = 0;
    var x = document.getElementById('btnPrintBoxCode');
    $(document).ready(function () {

        if ($("#PrintWay").attr("checked") == "checked") radioCount = 1;
        $("input[type='checkbox']").click(function () {
            if (radioCount == 1) {
                $(this).removeAttr("checked");
                $("#divBatch").css("visibility", "hidden");
                x.innerHTML = '打印一个盒码';
                radioCount = 0;
            } else {
                $(this).attr("checked", "checked");
                $("#divBatch").css("visibility", "visible");
                x.innerHTML = '生成盒码';
                radioCount = 1;
            }
        });

    });


    $("#archive-year").change(function () {
        $('#btnPrintBoxCode').prop("disabled", false);  // 启用
    });

     function init() {
         ys.ajax({
             url: '@Url.Content("~/StorageManage/BarLib/GetReprintCodes")',
             type: 'post',
             data: {"frompage": "Circulation"},
             success: function (obj) {
                        if (obj.Tag == 1) {
                            $('#box-list-for-reprint').html(obj.Result);
                        }
                        else {
                            ys.msgError(obj.Message);
                        }
                    }
                });
    }


    $("#archive-year").change(function () {
        $('#btnPrintBoxCode').prop("disabled", false);  // 启用
        var postData = $('#form').getWebControls();
        postData.frompage = "Circulation";
        console.log(postData);

         ys.ajax({
                    url: '@Url.Content("~/StorageManage/BarLib/GetBoxCodesListNotFinished")',
                    type: 'post',
                    data: postData,
                    success: function (obj) {
                        if (obj.Tag == 1) {
                            var defaultData = {};
                            defaultData.Description = obj.Description;

                            $('#collapseTwo').setWebControls(defaultData);

                            $('#collapseTwo').collapse('show');

                            //if (obj.TotalCount == 10) {  //未归属到 箱码 的盒码数达到10个， 禁止 打印盒码  按钮，必须要打印 箱码
                            //    $('#btnPrintBoxCode').prop("disabled", true);  // 禁用

                            //    $("#divBottom").css("visibility", "visible");
                            //    $("#divSecure").css("visibility", "hidden");
                            //}
                            //else {
                            //    $("#divBottom").css("visibility", "hidden");
                            //    $("#divSecure").css("visibility", "hidden");

                            //    $('#btnPrintBoxCode').prop("disabled", false);  // 启用
                            //}
                        }
                        else {
                            ys.msgError(obj.Message);
                        }
                    }
                });
    });
    //created by LJK 2021-7-7

    //如果四个框都有数据，显示建表按钮

    function checkContent() {
        var postData = $('#form').getWebControls();
        var start = postData.ArchiveIdStart;
        var end = postData.ArchiveIdEnd;
        var start2 = postData.ArchiveIdStartChild;
        var end2 = postData.ArchiveIdEndChild;

        @*var x = postData.ArchiveYear;
        var selecttext = $('#archive-year').find("option:selected").text();
        console.log(selecttext);
        console.log(x);
        var selecttext = postData.ArchiveYear.find("option:selected").text();
        var y = postData.ArchiveYear.val();
        console.log(selecttext);
        console.log(y);*@
            @*if ($("#archive-id-start").val() != '' && $("#archive-id-end").val() != ''
            && $("#archive-id-start-child").val() != '' && $("#archive-id-end-child").val() != '' && selecttext != "请选择") {
            $('#focobtn').attr('disabled', false);
        }
        else {
            $('#focobtn').attr('disabled', true);
        }*@
        if (start != '' && end != '' && start2 != '' && end2 != '')
            $('#focobtn').attr('disabled', false);
        else
            $('#focobtn').attr('disabled', true);


    }

    //created by LJK 2021-7-7
    //初始化暂存表数组函数，生成档案号,填入数组

    String.prototype.padLeft = function (len, charStr) {
        var s = this + '';
        return new Array(len - s.length + 1).join(charStr || '') + s;
    }
    String.prototype.padRight = function (len, charStr) {
        var s = this + '';
        return s + new Array(len - s.length + 1).join(charStr || '');
    }

    //created by LJK 2021-7-6

    function generateArcIds() {
        var postData = $('#form').getWebControls();
        var start = parseInt(postData.ArchiveIdStart);
        var end = parseInt(postData.ArchiveIdEnd);
        var start2 = parseInt(postData.ArchiveIdStartChild);
        var end2 = parseInt(postData.ArchiveIdEndChild);

        var arr = new Array();

        /*添加list的字段*/
        for (var i = start; i <= end; i++) {
            for (var j = start2; j <= end2; j++) {
                arr.push(postData.ArchiveType + "·" + postData.ArchiveYear + "·" + i.toString().padLeft(5, "0") + "·" + j.toString().padLeft(2, "0"));

            }
        }
        displaylist(arr);
        document.getElementById("arcIds").innerText = arr.toString();
    }

    //created by LJK 2021-7-7

    function displaylist(arr) {
        /*
            "<div style=\"text-align: center;margin-top: 0px\">"Z49·2020·00001.01 <a class="btn btn-danger  btn-sm" onclick="listdelete(    )"><i class="fa fa-remove"></i></a></div>
            <div style="text-align: center;margin-top: 5px">Z49·2020·00001.01 <a class="btn btn-danger  btn-sm" onclick="listdelete()"><i class="fa fa-remove"></i></a></div>
            <div style="text-align: center;margin-top: 5px">Z49·2020·00001.01 <a class="btn btn-danger  btn-sm" onclick="listdelete()"><i class="fa fa-remove"></i></a></div>
            <div style="text-align: center;margin-top: 5px">Z49·2020·00001.01 <a class="btn btn-danger  btn-sm" onclick="listdelete()"><i class="fa fa-remove"></i></a></div>
            <div style="text-align: center;margin-top: 5px">Z49·2020·00001.01 <a class="btn btn-danger  btn-sm" onclick="listdelete()"><i class="fa fa-remove"></i></a></div>
            <div style="text-align: center;margin-top: 5px">Z49·2020·00001.01 <a class="btn btn-danger  btn-sm" onclick="listdelete()"><i class="fa fa-remove"></i></a></div>
        */

        //console.log(arr);

        var temp = "", id;
        var arrstr = arr.toString();
        console.log(arrstr);
        for (j = 0; j < arr.length; j++) {
            id = arr[j];
            if (j == 0)
                temp += "<div style='text-align: center;margin-top: 0px'>" + id.substring(9, 20) + "<a class='btn btn-danger  btn-sm' onclick='listdelete(\"" + id + "\",\"" + arrstr + "\")'>" + " <i class='fa fa-remove'></i></a></div>";
            else
                temp += "<div style='text-align: center;margin-top: 5px'>" + id.slice(9, 20) + "<a class='btn btn-danger  btn-sm' onclick='listdelete(\"" + id + "\",\"" + arrstr + "\")'>" + " <i class='fa fa-remove'></i></a></div>";

        }


        console.log(temp);

        $("#archiveids").html(temp);

        $("#focotable").css("visibility", "visible");

    }
    //created by LJK 2021-7-7
    //暂存表内删除点击函数，从数组中删除对应档案号，再次调用呈现函数，将数组绑定到页面

    function listdelete(id, arrstr) {
        ys.confirm("确认要删除选中的这条档案吗？", function () {
            var arr = new Array();

            arr = arrstr.split(",");

            arr.remove(id);

            displaylist(arr);
            document.getElementById("arcIds").innerText = arr;
        });
    }

    Array.prototype.remove = function (val) {
        var index = this.indexOf(val);
        if (index > -1) {
            this.splice(index, 1);
        }
    };


    //打印盒码
    function printBoxCode() {

        if ($('#form').validate().form()) {
         var postData = $('#form').getWebControls();

         if (postData.ArchiveType == "") {
             ys.alertWarning("请选择档案类别。");
             return false;
         }
         if (postData.ArchiveYear == "") {
             ys.msgWarning("请选择档案年份。");
             return false;
         }
            if (postData.ArchiveIdStartChild == "" && postData.ArchiveIdEndChild != "") {
                ys.msgWarning("档案子号起始号为空时不能填入档案子号截止号！");
                return false;
            }

            postData.ArrayIds = document.getElementById("arcIds").innerText;

            // 批量打印
            if (radioCount == 0)
                postData.PrintWay = 0;
            else
                postData.PrintWay = 1;

         ys.ajax({
                    url: '@Url.Content("~/StorageManage/Circulation/PrintBoxCode")',
                    type: 'post',
                    data: postData,
                    success: function (obj) {
                        if (obj.Tag == 1) {
                            ys.msgSuccess(obj.Message);

                            var defaultData = {};
                            defaultData.Description = obj.Description;

                            $('#collapseTwo').setWebControls(defaultData);

                            $('#collapseTwo').collapse('show');
                            if (obj.TotalCount >= 10) {  //未归属到 箱码 的盒码数达到10个， 禁止 打印盒码  按钮，必须要打印 箱码
                                $('#btnPrintBoxCode').prop("disabled", true);  // 禁用
                            }
                            //    $("#divBottom").css("visibility", "visible");
                            //    $("#divSecure").css("visibility", "visible");
                            //}
                            //else {
                            //    $("#divBottom").css("visibility", "hidden");
                            //    $("#divSecure").css("visibility", "hidden");

                            //    $('#btnPrintBoxCode').prop("disabled", false);  // 启用
                            //}
                        }
                        else {
                            ys.msgError(obj.Message);
                        }
                    }
                });
        }
    }

     //批量打印盒码（不生成数据记录）
    function BatchprintBoxCode() {
        var boxcodes = document.getElementById('description').innerText;
        ys.ajax({
            url: '@Url.Content("~/StorageManage/BarLib/BatchPrintBoxCode")' + '?boxCodes=' + boxcodes,
                        type: 'post',
                        success: function (obj) {
                            if (obj.Tag == 1) {
                                ys.msgSuccess(obj.Message);
                            }
                            else {
                                if (obj.Message === "Settings to access printer '' are not valid.") {
                                    ys.msgError("没有检测到打印机，请检查打印机状态。")
                                } else
                                ys.msgError(obj.Message);
                            }
                        }
                    });
    }

    //为10个盒码设置周转箱号码
    function bindTempBinCode() {
        if ($('#form').validate().form()) {
            var postData = $('#form').getWebControls();
            ys.ajax({
                url: '@Url.Content("~/StorageManage/Circulation/BindTempBinCode")',
                type: 'post',
                data: postData,
                success: function (obj) {
                    if (obj.Tag == 1) {
                        ys.msgSuccess(obj.Message);

                        resetPage();
                        var defaultData = {};
                        defaultData.CurBinCode = obj.Result;    // 后端返回的当前箱码
                        defaultData.EntityId = obj.Description;  // 后端返回的 实体 Id (主键)

                        $('#divBinCode').setWebControls(defaultData);
                        $("#divSecure").css("visibility", "visible");
                    }
                    else {
                        ys.msgError(obj.Message);
                    }
                }
            });
        }
    }

    //完成一个装箱过程后，重置页面 -- 保留上次的档案类型、年份
    function resetPage() {
        $("#divSecure").css("visibility", "hidden");
        //$("#divBottom").css("visibility", "hidden");

        $("#archive-id-start").val("");
        $("#archive-id-start-child").val("");
        $("#archive-id-end-child").val("");

        $("#archive-id-end").val("");

        $("#curbincode").html("");
        $("#entityid").html("");

        $("#description").html("");

        $('#btnPrintBoxCode').prop("disabled", false); // 禁用

   //     $("#collapseTwo").css("collapse", "hiden");
    }
</script>